// JavaScript Document

function  calcAllCommiss()
{
//	alert(now_str__format_local());
	var today_str=today_local();
	var today_start=today_str+" 00:00:01";
	var today_end=today_str+" 23:59:59";
	
	
//	var pid=UrlParm.parm('parent_id');
	
	 var code="new(com.attilax.agent.AgentRechargeService).getSubMemTotalByAgentId_ByAgtOnly($pid$,$sdd$,$end$)";
 code=code.replace("$pid$",uid_cur);
  code=code.replace("$sdd$",today_start);
   code=code.replace("$end$",today_end);
 
	   var jsb=new AtiJsBridge();
	
		jsb.exe("$code="+encodeURIComponent(code),function(data){
			console.log(data);
			 try{
			  processJavaEx_V2q315(data);
		   }catch(e)
		   {
			 
				  showErr(e);
				  return;
		  
		   }
		   
		   $("#todaySum").text(data);
		    $("#mycommiss").html(  calcCommission( data));
		  	//  var json=str2json(data);
//		  
//			  var list=new TableV3("table1");
//		 
//			   list.bindData(json);
		  
		}); 
		
	//	ini_mem_total();
	
}
//get user info
function page_load()
{
	
	var orm=new atiOrmV4();
	orm.obj=" select * from agent left JOIN account on agent.uid=account.id where uid='$uid$$'";   // select sum(rmb) from recharge
	orm.login_url="../login/login.htm";
	orm.metaParam="$utype=agent";
	orm.callbackFun=function(data)
	{
		
		try{
			  processJavaEx_V2q315(data);
		   }catch(e)
		   {
			 
				if(catchEx(e,"com.attilax.user.NotLoginEx"))
				  {
					 
						logx("not login");
					//	window.location=;
						return;
						//
				  }
		  
		   }
		   if(data=="[]")
		   {
			window.location="../login/login.htm";
			return;   
		   }
		      var json=str2json(data);
		 
		   var obj=json[0];
		   var fm=new AForm()
		   fm.bind(obj);
		   $("#AgentsHeader_imgFace").attr("src",obj.icon);
		   	
	};
	
	  if(urole!="admin")
		 orm.query();
	
	/*var code="new(com.attilax.agent.AgentService).getCurUserinfo()";
	//code=(code);
	var jsb=new AtiJsBridge();
	jsb.exe("$code="+encodeURIComponent(code),function(data){
			alert(data);
			 try{
			  processJavaEx_V2q315(data);
		   }catch(e)
		   {
			 
				if(catchEx(e,"com.attilax.user.NotLoginEx"))
				  {
					 
						logx("not login");
						window.location="../login/login.htm";
						return;
						//
				  }
		  
		   }	
	});*/
	
	 getuid(function(){
		 my_mem_rechg_list();
		 
	 }   );todayChargeCalc();
	// set_sub_total_a();
	 drktl_player_count();
	 
	  writeCookie("index_url","../uc/uc.htm",9);
	 writeCookie("ck1","ck1val");
//	 alert(   document.cookie);
	 console.log("----read cook index_url:"+readCookie("index_url"));
	 calcAllCommiss();
}

function drktl_player_count()
{
	
	var sql="select count(id) as mymems  from account where promoter=$uid$";
	
		var orm=new atiOrmV4();
	orm.obj=sql;	
	orm.login_url="../login/login.htm";
	orm.metaParam="$utype=agent";
	orm.callbackFun=function(data)
	{
		
		try{
			  processJavaEx_V2q315(data);
		   }catch(e)
		   {
			 
				if(catchEx(e,"com.attilax.user.NotLoginEx"))
				  {
					 
						logx("not login");
					//	window.location=;
						return;
						//
				  }
		  
		   }
		   
		   var json=str2json(data);
		 
		   var obj=json[0];
		   if( getJsonLength(obj)==0)
			   obj.mymems=0;
		   var fm=new AForm()
		   fm.bind(obj);
		   	
	};
	
	//  if(urole=="admin")
		 orm.query();
	
	
	
}

/*
function set_sub_total_a()
{
	
//	var s=$("#sub_total_a").attr("href");
//	 
// 
//		   if(urole=="admin")
//		   		  s=s.replace("$pid$",0);
//			 else
//		      s=s.replace("$pid$",uid);
//		   $("#sub_total_a").attr("href",s);
		   
		   var s2=$("#my_mem_a").attr("href");
		      if(urole=="admin")
		   		  s2=s2.replace("$pid$",0);
			 else
				  s2=s2.replace("$pid$",uid);
		   $("#my_mem_a").attr("href",s2);
		 
	 
	
}
*/

function my_mem_rechg_list()
{
	
//	var s=$("#sub_total_a").attr("href");
//	 
// 
//		   if(urole=="admin")
//		   		  s=s.replace("$pid$",0);
//			 else
//		      s=s.replace("$pid$",uid);
//		   $("#sub_total_a").attr("href",s);
		   
		   var s2=$("#my_mem_a").attr("href");
		      if(urole=="admin")
		   		  s2=s2.replace("$pid$",0);
			 else
				  s2=s2.replace("$pid$",uid);
		   $("#my_mem_a").attr("href",s2);
		 
	 
	
}

function getJsonLength(jsonData){ var jsonLength = 0; for(var item in jsonData){ jsonLength++; } return jsonLength; }
function  todayChargeCalc()
{
	// alert(todayx());
		var today_str=today_local();
	var today_start=today_str+" 00:00:01";
	var today_end=today_str+" 23:59:59";
	var sql=" select sum(rmb) as todaySum from acc_rechg where `time`>'$start$' and `time`<'$end$'";   // select sum(rmb) from recharge
	sql=sql.replace("$start$",today_start);
	sql=sql.replace("$end$",today_end);
	var orm=new atiOrmV4();
	orm.obj=sql;	
	orm.login_url="../login/login.htm";
	orm.metaParam="$utype=agent";
	orm.callbackFun=function(data)
	{
		
		try{
			  processJavaEx_V2q315(data);
		   }catch(e)
		   {
			 
				if(catchEx(e,"com.attilax.user.NotLoginEx"))
				  {
					 
						logx("not login");
					//	window.location=;
						return;
						//
				  }
		  
		   }
		   
		      var json=str2json(data);
		 
		   var obj=json[0];
		   if( getJsonLength(obj)==0)
			   obj.todaySum=0;
		   var fm=new AForm()
		   fm.bind(obj);
		   $("#mycommiss").html(  calcCommission( obj.todaySum));
		   
		   	
	};
	
	//  if(urole=="admin")
	//	 orm.query();
	
}

function getuid(aftFun)
{
	
	var s=$("#sub_total_a").attr("href");
	 
	 var code="new(com.attilax.token.TokenServiceV2).setUtype(agent).getuid()";
 
	   var jsb=new AtiJsBridge();
	
		jsb.exe("$code="+encodeURIComponent(code),function(data){
			uid=data;
			console.log(data);
			 try{
			  processJavaEx_V2q315(data);
		   }catch(e)
		   {
			 
				  showErr(e);
				  return;
		  
		   }
		 //  alert(urole);
		   if(urole=="admin")
		   {
		 	   s=s.replace("$pid$",0);
			   $("#uid").text(data);
			   
		   }
			else
		 	  s=s.replace("$pid$",data);
		   $("#sub_total_a").attr("href",s);
		   
		   aftFun();
		   
		  // var s2=$("#my_mem_a").attr("href");
		//  s2=s2.replace("$pid$",data);
		//   $("#my_mem_a").attr("href",s2);
		});
	 
	
}